package com.princess.demok.ui.demo

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.graphics.drawable.ColorDrawable
import android.text.InputType
import android.view.View
import android.view.WindowManager
import android.widget.RadioGroup
import com.princess.demok.R
import com.princess.demok.base.view.MyBaseActivity
import com.princess.demok.ui.keyboard.KeyboardType
import com.princess.demok.ui.keyboard.OnNumberKeyboardListener
import com.princess.demok.ui.keyboard.XNumberKeyboardView
import kotlinx.android.synthetic.main.activity_keyboard.*

/**
 * @author: xha
 * @date: 2021/7/7 10:22
 * @Description:
 */
class KeyBoardActivity : MyBaseActivity(), com.princess.demok.ui.keyboard.OnNumberKeyboardListener,
    RadioGroup.OnCheckedChangeListener {

    companion object {
        @JvmStatic
        fun start(
            context: Activity
        ) {
            val starter = Intent(context, KeyBoardActivity::class.java)
            context.startActivity(starter)
        }
    }


    override fun getLayoutId(): Int {
        return R.layout.activity_keyboard
    }

    override fun initOnCreate() {

        window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

        rg_keyboard_type.setOnCheckedChangeListener(this)

        view_keyboard.setOnNumberKeyboardListener(this)

        btn1.setOnClickListener { view_keyboard.shuffleKeyboard() }

        btn2.setOnClickListener { view_keyboard.shuffleKeyboard(false) }

    }

    override fun onNumberKey(keyCode: Int, insert: String?) {

        // 右下角按键的点击事件，删除一位输入的文字
        if (keyCode == com.princess.demok.ui.keyboard.XNumberKeyboardView.KEYCODE_BOTTOM_RIGHT) {
            val start: Int = edit_text.length() - 1
            if (start >= 0) {
                edit_text.text.delete(start, start + 1)
            }
        } else {
            edit_text.append(insert)
        }
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    override fun onCheckedChanged(group: RadioGroup?, checkedId: Int) {
        // 纯数字键盘
        when (checkedId) {
            R.id.rb_pure_number -> {
                view_keyboard.setKeyboardType(com.princess.demok.ui.keyboard.KeyboardType.number)
                view_keyboard.setSpecialKeyBackground(ColorDrawable(resources!!.getColor(R.color.colorKeyBlank)))
            }
            R.id.rb_digit -> {
                view_keyboard.setKeyboardType(com.princess.demok.ui.keyboard.KeyboardType.digit)
                view_keyboard.setSpecialKeyBackground(resources!!.getDrawable(R.drawable.selector_key_background))
            }
            R.id.rb_idcard -> {
                view_keyboard.setKeyboardType(com.princess.demok.ui.keyboard.KeyboardType.idCard)
                view_keyboard.setSpecialKeyBackground(resources!!.getDrawable(R.drawable.selector_key_background))
            }
        }
    }
}